<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Haircoloroptions extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('haircoloroption');
	}
	public function ajax_haircoloroption_get(){
		$haircoloroptionEntity =  new CI_Hair_color_option_entity();
		$haircoloroption = $haircoloroptionEntity->getHairColorOptionByCode($_REQUEST['code']);
		echo json_encode($haircoloroption);
	}
	public function ajax_haircoloroption_list(){
		$haircoloroptionEntity =  new CI_Hair_color_option_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$haircoloroptionList = $haircoloroptionEntity->getHairColorOptionListByLanguage($defaultLanguageId);
		$data['haircoloroptionList'] = $haircoloroptionList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/haircoloroptions/index', $data);
	}
	public function ajax_haircoloroption_save(){
		$haircoloroptionNameList = $_REQUEST['haircoloroption'];
		$haircoloroptionNameDefault = '';
		foreach ($haircoloroptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$haircoloroptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($haircoloroptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_haircoloroption_require');
		}else{
			$code = 0;
			foreach ($haircoloroptionNameList as $key => $value) {
				$haircoloroptionEntity =  new CI_Hair_color_option_entity();
				if($value != null && $value != ''){
					$haircoloroptionEntity->setHairColor($value);
				}else{
					$haircoloroptionEntity->setHairColor($haircoloroptionNameDefault);
				}
				$haircoloroptionEntity->setLanguageId($key);
				$haircoloroptionEntity->setCode($code);
				$id = $haircoloroptionEntity->save();
				if($code == 0){
					$code = $id;
					$haircoloroptionEntity->setId($id);
					$haircoloroptionEntity->setCode($code);
					$haircoloroptionEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_haircoloroption_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_haircoloroption_update(){
		$haircoloroptionNameList = $_REQUEST['haircoloroption'];
		$haircoloroptionIdList = $_REQUEST['haircoloroption_id'];
		$haircoloroptionCode = $_REQUEST['haircoloroption_code'];
		$haircoloroptionNameDefault = '';
		foreach ($haircoloroptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$haircoloroptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($haircoloroptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_haircoloroption_require');
		}else{
			foreach ($haircoloroptionNameList as $key => $value) {
				$haircoloroptionEntity =  new CI_Hair_color_option_entity();
				if($value != null && $value != ''){
					$haircoloroptionEntity->setHairColor($value);
				}else{
					$haircoloroptionEntity->setHairColor($haircoloroptionNameDefault);
				}
				$haircoloroptionEntity->setLanguageId($key);
				$haircoloroptionEntity->setCode($haircoloroptionCode);
				$haircoloroptionEntity->setId($haircoloroptionIdList[$key]);
				$haircoloroptionEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_haircoloroption_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_haircoloroption_delete(){
		$result = array();
		$haircoloroptionEntity =  new CI_Hair_color_option_entity();
		$result['result'] = $haircoloroptionEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_haircoloroption_delete_success');
		}else{
			$result['message'] = lang('escort_haircoloroption_delete_error');
		}
		echo json_encode($result);
	}
}
?>